package com.koron.common;

import java.lang.reflect.Method;
import java.util.*;
import java.util.Map.Entry;

import javax.swing.JTextField;

import com.koron.common.MyCodeAssistant.ReplacePair;

public class CommonAssistant {
	/**
	 * mybatis方法文本框代码助手
	 * 
	 * @return
	 */
	public static MyCodeAssistant getMethodAssis() {
		MyCodeAssistant mca = new MyCodeAssistant();
		mca.getListData().add(MyCodeAssistant.newPair("@Param(\"$0" + MyCodeAssistant.CURSOR + "\")String $0"));
		MyCodeAssistant.ReplacePair pair = new MyCodeAssistant.ReplacePair("(.*)\\s(.*)", "@Param(\"$2" + MyCodeAssistant.CURSOR + "\")$1 $2");
		mca.getListData().add(pair);
		return mca;
	}

	/**
	 * mybatisSQL文本框助手
	 * 
	 * @return
	 */
	public static MyCodeAssistant getSqlAssis() {
		String pat = "<selectKey resultType=\"java.lang.Short\" order=\"BEFORE\" keyProperty=\"id\">" + MyCodeAssistant.CURSOR + "</selectKey>";
		MyCodeAssistant mca = new MyCodeAssistant();
		List<String> list = new ArrayList<String>();
		list.add(pat);
		list.add("<trim prefix=\"where\" prefixOverrides=\"and|or\">" + MyCodeAssistant.CURSOR + "</trim>");
		list.add("<if test=\"$0!=null and $0!=''\">and $0=" + MyCodeAssistant.CURSOR + "#{$0}</if>");
		list.add("<foreach collection=\"array\" index=\"index\" item=\"item\" close=\")\" open=\"(\" separator=\",\">#{item}</foreach>");
		list.add("<choose>" + MyCodeAssistant.CURSOR + "</choose>");
		list.add("<when test=\"$0 != null and $0 !=''\">" + MyCodeAssistant.CURSOR + "</when>");
		for (String string : list) {
			mca.getListData().add(MyCodeAssistant.newPair(string));
		}
		return mca;
	}
	/**
	 * 根据类名选择方法名
	 * @param jtfClassName 类名所在的文本框
	 * @param loader 类加载器
	 * @return 代码助手
	 */
	public static MyCodeAssistant getJavaMethodAssis(final JTextField jtfClassName,final ClassLoader loader) {
		MyCodeAssistant mca = new MyCodeAssistant() {
			@Override
			public List<ReplacePair> getSyncList() {
				try {
					Class<?> c = loader.loadClass(jtfClassName.getText());
					Method[] methods = c.getMethods();
					List<ReplacePair> list = new ArrayList<>();
 					for (Method method : methods) {
						list.add(new ReplacePair("^.*$", method.getName()));
					}
					return list;
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
				return super.getSyncList();
			}
		};
		return mca;
	}
	/**
	 * 根据Map自动生成${KEY.VALUE}格式的代码
	 * @param map 数据
	 * @return 
	 */
	public static MyCodeAssistant getDefineAssis(Map<String,List<String>> map) {
		MyCodeAssistant mca = new MyCodeAssistant();
		mca.getListData().add(MyCodeAssistant.newPair("{type:\"data\",data:["+MyCodeAssistant.CURSOR+"]}"));
		mca.getListData().add(MyCodeAssistant.newPair("{type:\"ajax\",url:\""+MyCodeAssistant.CURSOR+"\"}"));
		mca.getListData().add(MyCodeAssistant.newPair("(.*)\\s(.*)","{key:\"$1\",value:\"$2\"}"));
		mca.getListData().add(MyCodeAssistant.newPair("(.+)","{key:\"$1\",value:\""+MyCodeAssistant.CURSOR+"\"}"));
		mca.getListData().add(MyCodeAssistant.newPair("{key:\""+MyCodeAssistant.CURSOR+"\",value:\"\"}"));
		for (Entry<String,List<String>> item : map.entrySet()) {
			for (String string : item.getValue()) {
				ReplacePair pair = MyCodeAssistant.newPair("\\$\\{"+item.getKey()+"."+string+"\\}");
				pair.setGroup(item.getKey());
				mca.getListData().add(pair);
			}
		}
		return mca;
	}
	
	public static MyCodeAssistant getDefineAssis(List<String> list) {
		MyCodeAssistant mca = new MyCodeAssistant();
		for (String string : list) {
			mca.getListData().add(MyCodeAssistant.newPair("(.+"+string+".+)",string));
		}
		return mca;
	}
}
